const data = [
  { id: 1, name: "张三" },
  { id: 11, name: "张三1", parentId: 1 },
  { id: 111, name: "张三2", parentId: 11 },
  { id: 112, name: "张三3", parentId: 11 },
  { id: 2, name: "李四" },
  { id: 21, name: "李四1", parentId: 2 },
  { id: 211, name: "李四2", parentId: 22 },
  { id: 212, name: "李四3", parentId: 22 },
];
const result = data.reduce(function (prev, curr, index, item) {
  curr.children = item.filter((v) => v.parentId === curr.id);
  if (!curr.parentId) {
    prev.push(curr);
  }
  return prev;
}, []);

console.log(result);
